package com.mengrv.springbootdemo;

import com.alibaba.fastjson.JSONObject;
import com.mengrv.springbootdemo.model.Response;
import com.mengrv.springbootdemo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@SpringBootApplication
public class SpringbootdemoApplication {

    public static void main(String[] args) {
        //控制热更新
        System.setProperty("spring.devtools.restart.enabled", "true");
        SpringApplication.run(SpringbootdemoApplication.class, args);
    }

    //拦截器配置
    @Configuration
    static class WebMvcConfigurer extends WebMvcConfigurerAdapter {
        @Autowired
        UserService userService;

        public void addInterceptors(InterceptorRegistry registry) {
            registry.addInterceptor(new HandlerInterceptorAdapter() {
                @Override
                public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
                    /**
                     * token验证，登录、注册、修改密码时不需要验证token
                     * 经过过滤器的接口 [/users/**, /store/**]
                     */
                    response.setContentType("application/json;charset=utf-8");
                    {
                        String apiPath = request.getServletPath();
                        String token = request.getHeader("token");
                        Response responseModel = new Response();
                        String[] noTokenPath = new String[]{
                                "/user/password",
                                "/store/list",
                                "/store/detail",
                        };
                        for (int i = 0; i < noTokenPath.length; i++) {
                            if (apiPath.equals(noTokenPath[i])) {
                                return true;
                            }
                        }
                        if (token == null) {
                            responseModel.setAll(null, "用户token缺失", 203);
                            response.getWriter().write(JSONObject.toJSONString(responseModel));
                            return false;
                        } else if (userService.getUserByToken(token) == null) {
                            responseModel.setAll(new Object(), "token失效，请获取有效的token值", 203);
                            response.getWriter().write(JSONObject.toJSONString(responseModel));
                            return false;
                        }
                        return true;
                    }
                }
            }).addPathPatterns("/store/**");
//                    .addPathPatterns("/users/**", "/store/**")
//                    .excludePathPatterns("/users/login", "/users/new");
        }
    }
}
